package com.nais.controllers;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.nais.authority.AuthorityContext;
import com.nais.domain.EcCampus;
import com.nais.domain.EcCampusWithBLOBs;
import com.nais.domain.EcSchool;
import com.nais.domain.EcSchoolCriteria;
import com.nais.mapper.EcCampusMapper;
import com.nais.mapper.EcSchoolMapper;

@Controller
public class PayController {
	
	@Autowired
	private EcCampusMapper			campusMapper;
	
	@Autowired
	private EcSchoolMapper			schoolMapper;
	
	public static class SchoolModel {
		private EcCampus		campus;
		private EcSchool		school;
		
		private String 			start;
		private String 			end;
		
		public EcCampus getCampus() {
			return campus;
		}
		public void setCampus(EcCampus campus) {
			this.campus = campus;
		}
		public EcSchool getSchool() {
			return school;
		}
		public void setSchool(EcSchool school) {
			this.school = school;
		}
		public String getStart() {
			return start;
		}
		public void setStart(String start) {
			this.start = start;
		}
		public String getEnd() {
			return end;
		}
		public void setEnd(String end) {
			this.end = end;
		}
		
		
	}
		
	@GetMapping("/statement")
	public String personal() {
		return "statement";
	}
	
	@GetMapping("/schools")
	public String schools(Model model) throws ParseException {
		
		List<SchoolModel> sms = new ArrayList<>();
		EcSchoolCriteria criteria = new EcSchoolCriteria();
		criteria.createCriteria();
		List<EcSchool> schools =  schoolMapper.selectByExample(criteria);
		for(EcSchool s : schools) {
			int cid = s.getCid();
			EcCampusWithBLOBs camp = campusMapper.selectByPrimaryKey(cid);
			if (camp != null) {
				SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
			    Long beginTime=new Long(camp.getBeginTime());  
			    String d = format.format(beginTime * 1000);
				SchoolModel sm = new SchoolModel();
				sm.setStart(d);
				Long endTime = new Long(camp.getEndTime());
				String e = format.format(endTime * 1000);
				sm.setEnd(e);
				sm.setCampus(camp);
				sm.setSchool(s);
				sms.add(sm);
			}
		}
		model.addAttribute("schools", sms);
		return "schools";
	}
	
//	@GetMapping("/schools/{id}")
//	public String school(@PathVariable("id") Integer id) {
//		
//	}
	
	@GetMapping("/corse-choose/{id}")
	public String courseChoose(@PathVariable("id") Integer id){
		
		return "corse-choose";
	}
	
	@GetMapping("/corse")
	public String courseChooset(@PathVariable("id") Integer id){
		
		return "corse-choose";
	}
	
	@GetMapping("/order-preview")
	public String orderPreview() {
		return "order-preview";
	}
	
	@GetMapping("/pay-choose")
	public String pay() {
		return "pay-choose";
	}
	
	
}
